//logs.js
var util = require('../../utils/util.js');
var app = getApp();
var globalBgAudioManager = util.backgroundAudioManager; 
var musicList;
var indexI;
var letCourse;
var qiehuan;//切换页面相关
Page({
    data: {
        indexI: 0,
        playbtn: false,//开始
        pausebtn: false,//暂停
        gifbtn:true,
        item_song: null, // 音乐地址
        timer: null,  //定时器
        ncount: 0,   // 动画旋转计数
        sliderMax:100,
        letCourse:[]
    },
    /**
    * 生命周期函数--监听页面加载
    */
    onLoad: function (options) {


    },
    // 滑块
    hanleSliderChange: function (e) {
        console.log(e)
        // util.backgroundAudioManager.currentTime / util.backgroundAudioManager.duration * 100,
        if (!globalBgAudioManager.duration)
            return;

        let time = globalBgAudioManager.duration * e.detail.value / 100;
        console.log(time);
        globalBgAudioManager.seek(time);
    },
    // 分享功能
    onShareAppMessage: function (options) {
        var that = this;
        // 设置菜单中的转发按钮触发转发事件时的转发内容
        var shareObj = {
            title: "i幸会小程序",        // 默认是小程序的名称(可以写slogan等)
            path: '/pages/main/main',        // 默认是当前页面，必须是以‘/’开头的完整路径
            imageUrl: '../img/touxiang.jpg',     //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
            success: function (res) {
                // 转发成功之后的回调
                if (res.errMsg == 'shareAppMessage:ok') {
                    console.log(res);
                    wx.showShareMenu({
                        withShareTicket: true,
                        success: function () {
                            console.log(11111);
                        }
                    })
                }
            },
            fail: function (res) {
                // 转发失败之后的回调
                if (res.errMsg == 'shareAppMessage:fail cancel') {
                    // 用户取消转发
                } else if (res.errMsg == 'shareAppMessage:fail') {
                    // 转发失败，其中 detail message 为详细失败信息
                }
            },
            complete: function () {
                // 转发结束之后的回调（转发成不成功都会执行）
            }
        };
        // 来自页面内的按钮的转发
        if (options.from == 'button') {
            var eData = options.target.dataset;
            console.log(options);
            console.log(eData.name);     // shareBtn
            // 此处可以修改 shareObj 中的内容
            shareObj.path = '/pages/btnname/btnname?btn_name=' + eData.name;
        }
        // 返回shareObj
        return shareObj;
    },
  // 上一首
  last: function (e) {
      let that=this;
      musicList = wx.getStorageSync('letLessonList').result;
      letCourse = wx.getStorageSync('letCourse').course;
      indexI=indexI-1;
      console.log(indexI);
      if (indexI<0){
          indexI=0;
          // 提示
          wx.showToast({
              title: '已经是第一条',
              icon: 'none',
              duration: 1000
          }) 
      }else{
          that.setData({
              musicList: musicList[indexI],
              indexI: indexI
          })
          if (globalBgAudioManager.paused) {
              that.play();
              setTimeout(function () {
                  globalBgAudioManager.pause();
              }, 1000)
          } else {
              that.play();
              that.setData({
                  playbtn: false,
                  pausebtn: false,
                  gifbtn: true
              });
              setTimeout(function () {
                  that.setData({
                      playbtn: false,
                      pausebtn: true,
                      gifbtn: false
                  });
              }, 1000)
          }
      }
  },
  // 下一首
  next: function (e) {
      let that = this;
    //   获取课程列表
      musicList = wx.getStorageSync('letLessonList').result;
      letCourse = wx.getStorageSync('letCourse').course;
      indexI = indexI + 1;
      console.log(indexI);
      if (letCourse.isLetPay){
          if (indexI >= musicList.length) {
              indexI = indexI - 1
              // 提示
              wx.showToast({
                  title: '已经是最后一条',
                  icon: 'none',
                  duration: 1000
              })
          } else {
              console.log(indexI);
              that.setData({
                  musicList: musicList[indexI],
                  indexI: indexI
              })
              if (globalBgAudioManager.paused) {
                  that.play();
                  setTimeout(function () {
                      globalBgAudioManager.pause();
                  }, 1000)
              } else {
                  that.play();
                  that.setData({
                      playbtn: false,
                      pausebtn: false,
                      gifbtn: true
                  });
                  setTimeout(function () {
                      that.setData({
                          playbtn: false,
                          pausebtn: true,
                          gifbtn: false
                      });
                  }, 1000)
              }
          }
      }else{
          if (indexI >= musicList.length) {
              indexI = indexI - 1;
              // 提示
              wx.showToast({
                  title: '已经是最后一条',
                  icon: 'none',
                  duration: 1000
              })
          } else if (musicList[indexI].isPublic==2){
              indexI = indexI - 1;
              wx.showToast({
                  title: '请先购买',
                  icon: 'none',
                  duration: 1000
              })
          }else {
              that.setData({
                  musicList: musicList[indexI],
                  indexI: indexI
              })
              if (globalBgAudioManager.paused) {
                  that.play();
                  setTimeout(function () {
                      globalBgAudioManager.pause();
                  }, 1000)
              } else {
                  that.play();
                  that.setData({
                      playbtn: false,
                      pausebtn: false,
                      gifbtn: true
                  });
                  setTimeout(function () {
                      that.setData({
                          playbtn: false,
                          pausebtn: true,
                          gifbtn: false
                      });
                  }, 1000)
              }
          }
      }
     
  },
  
  goPage:function(e){
      console.log(e);
        let url = e.currentTarget.dataset.url;
        let indexi = e.currentTarget.dataset.indexi;
        wx.navigateTo({
            url: url + "?indexi=" + e.currentTarget.dataset.indexi
        });
    },
// 播放
  playgame: function () {
      let that=this;
      globalBgAudioManager.play();

      that.setData({
          playbtn: false,
          pausebtn: false,
          gifbtn: true
      });
      setTimeout(function () {
          that.setData({
              playbtn: false,
              pausebtn: true,
              gifbtn: false
          });
      }, 1000)
      
  },
// 暂停
  pausegame: function () {
      let that=this;
      that.setData({
          playbtn: true,
          pausebtn: false,
          gifbtn: false
      });
      globalBgAudioManager.pause();
  },
  play:function(){
      globalBgAudioManager.src = musicList[indexI].mp3;
      console.log(musicList[indexI].mp3.replace("http", "https"));
      globalBgAudioManager.title = musicList[indexI].title;
      globalBgAudioManager.epname = musicList[indexI].title;
      globalBgAudioManager.singer = letCourse.user.name;
      globalBgAudioManager.coverImgUrl = letCourse.headAppletImageObj;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
      
     
  },
  //播放列表
  updatePlay:function(){

      let that = this;
      //   获取当节课程
      if (wx.getStorageSync('indexI')){
          indexI = wx.getStorageSync('indexI');
      }else{
          indexI=0;
      }
    
    //   获取音乐列表
      musicList = wx.getStorageSync('letLessonList').result;
      letCourse = wx.getStorageSync('letCourse').course;
   
      console.log(musicList);
      console.log(letCourse);
    //   顶部导航栏设置
    //   wx.setNavigationBarTitle({
    //       title: letCourse.name
    //   })
      that.setData({
          letCourse: letCourse,
          musicList: musicList[indexI],
          indexI: indexI
      })
      
      that.play();

      //   console.log(backgroundAudioManager.currentTime);
      that.playgame();
      //   背景音频播放进度更新事件
      globalBgAudioManager.onTimeUpdate(function () {
          //   console.log(globalBgAudioManager.currentTime)
          that.setData({
              valuecount: globalBgAudioManager.currentTime / globalBgAudioManager.duration * 100,
              currentProcess: util.shijian(globalBgAudioManager.currentTime),
              totalProcess: util.shijian(globalBgAudioManager.duration),
          });
      });
      //   背景音频自然播放结束事件
      globalBgAudioManager.onEnded(function () {
          if (letCourse.isLetPay) {
              if (indexI < musicList.length - 1) {
                  indexI = indexI + 1;
                //   console.log(indexI);
                  that.play();
                  that.setData({
                      musicList: musicList[indexI],
                      indexI: indexI
                  })
              } else {
                  indexI = 0;
                  that.play();
                  that.setData({
                      musicList: musicList[indexI],
                      indexI: indexI
                  })
              }
          }else{
              if (musicList[indexI+1].isPublic == 1) {
                  indexI = indexI + 1;
                  console.log(indexI);
                  that.play();
                  that.setData({
                      musicList: musicList[indexI],
                      indexI: indexI
                  })
              } else if (musicList[indexI].isPublic == 2){
                  indexI = 0;
                  that.play();
                  that.setData({
                      musicList: musicList[indexI],
                      indexI: indexI
                  })
              }else {
                  indexI = 0;
                  that.play();
                  that.setData({
                      musicList: musicList[indexI],
                      indexI: indexI
                  })
              }
          }
          
        


      });
      //   背景音频上一首
      globalBgAudioManager.onPrev(function () {
          that.last();
      });
      //下一首
      globalBgAudioManager.onNext(function () {
          that.next();
      });

      globalBgAudioManager.onPlay(function () {
          console.log("暂停");

      });
      globalBgAudioManager.onPause(function () {
          console.log('播放');
        
      });
      globalBgAudioManager.onStop(function () {
          console.log(1234);
      });
  },
 
  onShow: function () {
    //   app.checkSettingStatu();
      //切换页面相关
      qiehuan = wx.getStorageSync('qiehuan');
      if (qiehuan==0){
          this.updatePlay();
          wx.setStorageSync('qiehuan', 1)
      }
      
  },
 

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
      console.log('onHide')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
      wx.setStorageSync('indexI', 0)
  }
})
